﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.CMS
{
    public partial class HotdealProducts : System.Web.UI.Page
    {
        cmsProductCategoryBL proCateBL = new cmsProductCategoryBL();
        cmsProductBL proBL = new cmsProductBL();
        cmsProductCategoryDO objProCate = new cmsProductCategoryDO();
        private string countDownSettingString = "";
        public string startscript = "";
        public string endscript = "";
        private string countDownSettingString2 = "";
        public string startscript2 = "";
        public string endscript2 = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                SeoWithoutId();
            LoadData();
        }
        protected int GetLocationId()
        {
            int locationId = 0;
            if (Session["LocationId"] == null)
            {
                locationId = 0;
            }
            else
            {
                locationId = int.Parse(Session["LocationId"].ToString());
            }
            return locationId;
        }
        protected void LoadData()
        {
            string sort = ddlSort.SelectedValue;
            LoadProducts(proBL.SelectFrontPage(0, GetLocationId(), 0, -1, -1, -1, -1, 1), rptAllProduct, collectAllProducts, 12, sort);
            LoadProducts(proBL.SelectFrontPage(0, GetLocationId(), 0, -1, 1, -1, -1, 1), rptNewProducts, collectAllProducts, 12, sort);
        }
        protected void SeoWithoutId()
        {
            //SEO
            this.Title = " Voucher vinetlife | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);

            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }
      
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        private void LoadProducts(DataTable dt, Repeater rpt, SiteUtils.CollectionPager collect, int pagesize, string sort)
        {
            string sortBy = "";
            if (sort.Equals("prd"))
            {
                sortBy = "price desc";
            }
            else if (sort.Equals("pra"))
            {
                sortBy = "price asc";
            }
            else if (sort.Equals("proa"))
            {
                sortBy = "productid asc";
            }
            else if (sort.Equals("prod"))
            {
                sortBy = "productid desc";
            }

            if (dt.Rows.Count > 0)
            {
                collect.MaxPages = 10000;

                collect.PageSize = pagesize; // số items hiển thị trên một trang

                collect.DataSource = new DataView(dt, "", sortBy, DataViewRowState.CurrentRows);

                collect.BindToControl = rpt;
                rpt.DataSource = collect.DataSourcePaged;
                rpt.DataBind();
            }
            else { }
        }
        protected void rptAllProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Panel pnlCountdownWrap = (Panel)e.Item.FindControl("pnlCountdownWrap");
                Panel pnlCountDown = (Panel)e.Item.FindControl("pnlCountDown");

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }

                DateTime countDown = (DateTime)drv["PromotionEndDate"];
                if (countDown <= DateTime.Now)
                {
                    Label lblCountDownZero = new Label();
                    lblCountDownZero.CssClass = "countdown-time";
                    lblCountDownZero.Text = "00:00:00";
                    pnlCountDown.Controls.Add(lblCountDownZero);
                }
                else
                {
                    //pnlCountdownWrap.Visible = true;
                    startscript += "var austDay = new Date();austDay = new Date(" + countDown.Year + "," + (countDown.Month - 1) + "," + countDown.Day + "," + countDown.Hour + "," + countDown.Minute + "," + countDown.Second + ");$('#" + pnlCountDown.ClientID + "').countdown({ until: austDay,format: 'HMS',compact: true,description: ''});$('#year').text(austDay.getFullYear());";
                }
            }
            countDownSettingString = startscript + endscript;
        }
        protected void rptNewProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Panel pnlCountdownWrap = (Panel)e.Item.FindControl("pnalCountDownBox");
                Panel pnlCountDown = (Panel)e.Item.FindControl("pnlCountDown");

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }

                DateTime countDown = (DateTime)drv["PromotionEndDate"];
                if (countDown <= DateTime.Now)
                {
                    Label lblCountDownZero = new Label();
                    lblCountDownZero.CssClass = "countdown-time";
                    lblCountDownZero.Text = "00:00:00";
                    pnlCountDown.Controls.Add(lblCountDownZero);
                }
                else
                {
                    //pnlCountdownWrap.Visible = true;
                    startscript2 += "var austDay = new Date();austDay = new Date(" + countDown.Year + "," + (countDown.Month - 1) + "," + countDown.Day + "," + countDown.Hour + "," + countDown.Minute + "," + countDown.Second + ");$('#" + pnlCountDown.ClientID + "').countdown({ until: austDay,format: 'HMS',compact: true,description: ''});$('#year').text(austDay.getFullYear());";
                }
            }
            countDownSettingString2 = startscript2 + endscript2;
        }
        protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sortBy = ddlSort.SelectedValue;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int productCategoryId = int.Parse(Request.QueryString["ID"]);

            }
            else
            {

                //  LoadAllProducts();
            }
        }

        protected void rptAllProduct_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>" + countDownSettingString  + "</script>");
            }
            ScriptManager.RegisterClientScriptBlock(updateCate, updateCate.GetType(), "", countDownSettingString , true);
        }

        protected void rptNewProducts_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "show2", "<script>" + countDownSettingString2 + "</script>");
            }
            ScriptManager.RegisterClientScriptBlock(updateCate, updateCate.GetType(), "", countDownSettingString2, true);
        }
    }
}